package com.example.auth.controller;

import com.example.auth.service.UserService;
import com.example.common.dto.base.ResultMsg;
import com.example.common.dto.user.LoginReqDto;
import com.example.common.dto.user.UserDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 登录服务
 *
 * @author huangjie
 */
@Api(tags = "登录服务")
@Slf4j
@RestController
@RequestMapping(value = "/auth", method = RequestMethod.POST)
public class AuthController {
    @Autowired
    private UserService userService;

    @ApiOperation("注册接口")
    @RequestMapping(value = "/register")
    public ResultMsg<String> register(@ApiParam(value = "消息对象",required = true) @RequestBody(required = true) UserDto userDto) {
        userDto.verify(true);
        return userService.register(userDto);
    }

    @ApiOperation("登录接口")
    @RequestMapping(value = "/login")
    public ResultMsg<String> login(@ApiParam(value = "消息对象",required = true) @RequestBody(required = true)LoginReqDto loginReqDto) {
        loginReqDto.verify(true);
        return userService.login(loginReqDto);
    }

    @ApiOperation("登出接口")
    @RequestMapping(value = "/logout")
    public ResultMsg<Void> logout(@ApiParam(value = "userId", required = true) @RequestAttribute(value = "Authorization-UserId") String userId) {
         userService.logout(userId);
         return ResultMsg.generateSuccessResults("账号已退出");
    }

    @ApiOperation("测试接口")
    @RequestMapping(value = "/test")
    public ResultMsg<String> test(@RequestAttribute(value = "Authorization-UserId")String userId) {
        return ResultMsg.generateSuccessResults("测试成功", "userId="+userId);
    }
}
